1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 using System.Security.Cryptography;
11 using CrystalDecisions.Shared;
12 using CrystalDecisions.CrystalReports.Engine;
13 namespace WarehouseManagementSystem
14 {
15 public partial class frmPlaceOrders : Form
16 {
17 SqlCommand cmd;
18 SqlConnection con;
19 SqlDataReader rdr;
20 ConnectionString cs = new ConnectionString();
21
22 public frmPlaceOrders()
23 {
24 InitializeComponent();
25 }
26 private void auto()
27 {
28 txtInvoiceNo.Text = "OD-" + GetUniqueKey(8);
29
30 }
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
47
48 private void Save_Click(object sender, EventArgs e)
49 {
50 try
51 {
52 if (txtCustomerID.Text == "")
53 {
54 MessageBox.Show("Please retrieve Customer Details", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
55 return;
56 }
57
58 if (txtTaxPer.Text == "")
59 {
60 MessageBox.Show("Please enter tax percentage", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
61 txtTaxPer.Focus();
62 return;
63 }
64 if (txtDiscountPer.Text == "")
65 {
66 MessageBox.Show("Please enter discount percentage", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
67 txtDiscountPer.Focus();
68 return;
69 }
70 if (txtTotalPayment.Text == "")
71 {
72 MessageBox.Show("Please enter total payment", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
73 txtTotalPayment.Focus();
74 return;
75 }
76 if (cmbPaymentType.Text == "")
77 {
78 MessageBox.Show("Please select payment type", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
79 cmbPaymentType.Focus();
80 return;
81 }
82
83 if (ListView1.Items.Count == 0)
84 {
85 MessageBox.Show("sorry no product added", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
86 return;
87 }
88
89 auto();
90
91 con = new SqlConnection(cs.DBConn);
92 con.Open();
93
94 string cb = "insert Into Invoice_Info(InvoiceNo,InvoiceDate,CustomerID,SubTotal,VATPer,VATAmount,DiscountPer,DiscountAmount,GrandTotal,TotalPayment,PaymentDue,PaymentType,Status) VALUES ('" + txtInvoiceNo.Text + "','" + dtpInvoiceDate.Text + "','" + txtCustomerID.Text + "'," + txtSubTotal.Text + "," + txtTaxPer.Text + "," + txtTaxAmt.Text + ","+ txtDiscountPer.Text +","+ txtDiscountAmount.Text +"," + txtTotal.Text + "," + txtTotalPayment.Text + "," + txtPaymentDue.Text + ",'" + cmbPaymentType.Text + "','Confirmed')";
95 cmd = new SqlCommand(cb);
96 cmd.Connection = con;
97 cmd.ExecuteReader();
98 if (con.State == ConnectionState.Open)
99 {
100 con.Close();
101 }
102 con.Close();
103
104
105 for (int i = 0; i <= ListView1.Items.Count - 1; i++)
106 {
107 con = new SqlConnection(cs.DBConn);
108
109 string cd = "insert Into ProductSold(InvoiceNo,ProductID,ProductName,Quantity,Price,TotalAmount) VALUES (@d1,@d2,@d3,@d4,@d5,@d6)";
110 cmd = new SqlCommand(cd);
111 cmd.Connection = con;
112 cmd.Parameters.AddWithValue("d1", txtInvoiceNo.Text);
113 cmd.Parameters.AddWithValue("d2", ListView1.Items[i].SubItems[1].Text);
114 cmd.Parameters.AddWithValue("d3", ListView1.Items[i].SubItems[2].Text);
115 cmd.Parameters.AddWithValue("d4", ListView1.Items[i].SubItems[4].Text);
116 cmd.Parameters.AddWithValue("d5", ListView1.Items[i].SubItems[3].Text);
117 cmd.Parameters.AddWithValue("d6", ListView1.Items[i].SubItems[5].Text);
118 con.Open();
119 cmd.ExecuteNonQuery();
120 con.Close();
121 }
122 for (int i = 0; i <= ListView1.Items.Count - 1; i++)
123 {
124 con = new SqlConnection(cs.DBConn);
125 con.Open();
126 string cb1 = "update temp_stock set Quantity = Quantity - " + ListView1.Items[i].SubItems[4].Text + " where ProductID= '" + ListView1.Items[i].SubItems[1].Text + "'";
127 cmd = new SqlCommand(cb1);
128 cmd.Connection = con;
129 cmd.ExecuteNonQuery();
130 con.Close();
131 }
132
133 Save.Enabled = false;
134 btnPrint.Enabled = true;
135 GetData();
136 MessageBox.Show("Successfully Placed", "Order", MessageBoxButtons.OK, MessageBoxIcon.Information);
137 }
138 catch (Exception ex)
139 {
140 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
141 }
142 }
143 public void Getdata1()
144 {
145 con = new SqlConnection(cs.DBConn);
146 con.Open();
147 cmd = con.CreateCommand();
148
149 cmd.CommandText = "SELECT CustomerID,CustomerName FROM Customer WHERE CustomerID= '" + txtCustomerID.Text.Trim() + "'";
150 rdr = cmd.ExecuteReader();
151
152 if (rdr.Read())
153 {
154 txtCustomerID.Text = (rdr.GetString(0).Trim());
155 txtCustomerName.Text = (rdr.GetString(1).Trim());
156 }
157
158 if ((rdr != null))
159 {
160 rdr.Close();
161 }
162 if (con.State == ConnectionState.Open)
163 {
164 con.Close();
165 }
166 }
167 private void frmInvoice_Load(object sender, EventArgs e)
168 {
169 GetData();
170 Getdata1();
171 }
172
173
174 public void Calculate()
175 {
176 if (txtTaxPer.Text != "")
177 {
178 txtTaxAmt.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert.ToDouble(txtTaxPer.Text) / 100)).ToString();
179
180 }
181 if (txtDiscountPer.Text != "")
182 {
183 txtDiscountAmount.Text = Convert.ToInt32(((Convert.ToInt32(txtSubTotal.Text) + Convert.ToInt32(txtTaxAmt.Text)) * Convert.ToDouble(txtDiscountPer.Text) / 100)).ToString();
184 }
185 int val1 = 0;
186 int val2 = 0;
187 int val3 = 0;
188 int val4 = 0;
189 int val5= 0;
190 int.TryParse(txtTaxAmt.Text, out val1);
191 int.TryParse(txtSubTotal.Text, out val2);
192 int.TryParse(txtDiscountAmount.Text, out val3);
193 int.TryParse(txtTotal.Text, out val4);
194 int.TryParse(txtTotalPayment.Text, out val5);
195 val4 = val1 + val2 - val3;
196 txtTotal.Text = val4.ToString();
197 int I = (val4 - val5);
198 txtPaymentDue.Text = I.ToString();
199
200
201 }
202 private void txtSaleQty_TextChanged(object sender, EventArgs e)
203 {
204 int val1 = 0;
205 int val2 = 0;
206 int.TryParse(txtPrice.Text, out val1);
207 int.TryParse(txtSaleQty.Text, out val2);
208 int I = (val1 * val2);
209 txtTotalAmount.Text = I.ToString();
210 }
211
212 public double subtot()
213 {
214 int i = 0;
215 int j = 0;
216 int k = 0;
217 i = 0;
218 j = 0;
219 k = 0;
220
221
222 try
223 {
224
225 j = ListView1.Items.Count;
226 for (i = 0; i <= j - 1; i++)
227 {
228 k = k + Convert.ToInt32(ListView1.Items[i].SubItems[5].Text);
229 }
230
231 }
232
233 catch (Exception ex)
234 {
235 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
236 }
237 return k;
238
239 }
240
241 private void Button7_Click(object sender, EventArgs e)
242 {
243 try
244 {
245 if (txtCustomerID.Text == "")
246 {
247 MessageBox.Show("Please retrieve Customer ID", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
248 txtCustomerID.Focus();
249 return;
250 }
251
252 if (txtProductName.Text=="")
253 {
254 MessageBox.Show("Please retrieve product name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
255 return;
256 }
257 if (txtSaleQty.Text=="")
258 {
259 MessageBox.Show("Please enter no. of buy quantity", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
260 txtSaleQty.Focus();
261 return;
262 }
263 int SaleQty = Convert.ToInt32(txtSaleQty.Text);
264 if (SaleQty == 0)
265 {
266 MessageBox.Show("no. of sale quantity can not be zero", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
267 txtSaleQty.Focus();
268 return;
269 }
270
271 if (ListView1.Items.Count==0)
272 {
273
274 ListViewItem lst = new ListViewItem();
275 lst.SubItems.Add(txtProductID.Text);
276 lst.SubItems.Add(txtProductName.Text);
277 lst.SubItems.Add(txtPrice.Text);
278 lst.SubItems.Add(txtSaleQty.Text);
279 lst.SubItems.Add(txtTotalAmount.Text);
280 ListView1.Items.Add(lst);
281 txtSubTotal.Text = subtot().ToString();
282
283 Calculate();
284 txtProductName.Text = "";
285 txtProductID.Text = "";
286 txtPrice.Text = "";
287 txtAvailableQty.Text = "";
288 txtSaleQty.Text = "";
289 txtTotalAmount.Text = "";
290 txtProduct.Text = "";
291 return;
292 }
293
294 for (int j = 0; j <= ListView1.Items.Count - 1; j++)
295 {
296 if (ListView1.Items[j].SubItems[1].Text == txtProductID.Text)
297 {
298 ListView1.Items[j].SubItems[1].Text = txtProductID.Text;
299 ListView1.Items[j].SubItems[2].Text = txtProductName.Text;
300 ListView1.Items[j].SubItems[3].Text = txtPrice.Text;
301 ListView1.Items[j].SubItems[4].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[4].Text) + Convert.ToInt32(txtSaleQty.Text)).ToString();
302 ListView1.Items[j].SubItems[5].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[5].Text) + Convert.ToInt32(txtTotalAmount.Text)).ToString();
303 txtSubTotal.Text = subtot().ToString();
304 Calculate();
305 txtProductName.Text = "";
306 txtProductID.Text = "";
307 txtPrice.Text = "";
308 txtAvailableQty.Text = "";
309 txtSaleQty.Text = "";
310 txtTotalAmount.Text = "";
311 return;
312
313 }
314 }
315
316 ListViewItem lst1 = new ListViewItem();
317
318 lst1.SubItems.Add(txtProductID.Text);
319 lst1.SubItems.Add(txtProductName.Text);
320 lst1.SubItems.Add(txtPrice.Text);
321 lst1.SubItems.Add(txtSaleQty.Text);
322 lst1.SubItems.Add(txtTotalAmount.Text);
323 ListView1.Items.Add(lst1);
324 txtSubTotal.Text = subtot().ToString();
325 Calculate();
326 txtProductName.Text = "";
327 txtProductID.Text = "";
328 txtPrice.Text = "";
329 txtAvailableQty.Text = "";
330 txtSaleQty.Text = "";
331 txtTotalAmount.Text = "";
332 return;
333 }
334 catch (Exception ex)
335 {
336 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
337 }
338 }
339
340 private void btnRemove_Click(object sender, EventArgs e)
341 {
342 try
343 {
344 if (ListView1.Items.Count == 0)
345 {
346 MessageBox.Show("No items to remove", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
347 }
348 else
349 {
350 int itmCnt = 0;
351 int i = 0;
352 int t = 0;
353
354 ListView1.FocusedItem.Remove();
355 itmCnt = ListView1.Items.Count;
356 t = 1;
357
358 for (i = 1; i <= itmCnt + 1; i++)
359 {
360 //Dim lst1 As New ListViewItem(i)
361 //ListView1.Items(i).SubItems(0).Text = t
362 t = t + 1;
363
364 }
365 txtSubTotal.Text = subtot().ToString();
366 Calculate();
367 }
368
369 btnRemove.Enabled = false;
370 if (ListView1.Items.Count == 0)
371 {
372 txtSubTotal.Text = "";
373 }
374 }
375 catch (Exception ex)
376 {
377 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
378 }
379 }
380
381 private void txtTaxPer_TextChanged(object sender, EventArgs e)
382 {
383 try
384 {
385 if (string.IsNullOrEmpty(txtTaxPer.Text))
386 {
387 txtTaxAmt.Text = "";
388 txtTotal.Text = "";
389 return;
390 }
391 Calculate();
392 }
393 catch (Exception ex)
394 {
395 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
396 }
397 }
398
399 private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
400 {
401 btnRemove.Enabled = true;
402 }
403
404 private void textBox1_TextChanged(object sender, EventArgs e)
405 {
406 try
407 {
408 con = new SqlConnection(cs.DBConn);
409 con.Open();
410 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProduct.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
411 cmd = new SqlCommand(sql, con);
412 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
413 dataGridView1.Rows.Clear();
414 while (rdr.Read() == true)
415 {
416 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4]);
417 }
418 con.Close();
419 }
420 catch (Exception ex)
421 {
422 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
423 }
424 }
425
426 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
427 {
428 string strRowNumber = (e.RowIndex + 1).ToString();
429 SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
430 if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
431 {
432 dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
433 }
434 Brush b = SystemBrushes.ControlText;
435 e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
436
437 }
438
439 private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
440 {
441 try
442 {
443 DataGridViewRow dr = dataGridView1.SelectedRows[0];
444 txtProductID.Text = dr.Cells[0].Value.ToString();
445 txtProductName.Text = dr.Cells[1].Value.ToString();
446 txtPrice.Text = dr.Cells[3].Value.ToString();
447 txtAvailableQty.Text = dr.Cells[4].Value.ToString();
448 txtSaleQty.Focus();
449 }
450 catch (Exception ex)
451 {
452 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
453 }
454 }
455 public void GetData()
456 {
457 try
458 {
459 con = new SqlConnection(cs.DBConn);
460 con.Open();
461 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0) order by ProductName";
462 cmd = new SqlCommand(sql, con);
463 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
464 dataGridView1.Rows.Clear();
465 while (rdr.Read() == true)
466 {
467 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3],rdr[4]);
468 }
469 con.Close();
470 }
471 catch (Exception ex)
472 {
473 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
474 }
475 }
476 private void Reset()
477 {
478 txtInvoiceNo.Text = "";
479 cmbPaymentType.Text = "";
480 dtpInvoiceDate.Text = DateTime.Today.ToString();
481 txtCustomerID.Text = "";
482 txtCustomerName.Text = "";
483 txtProductName.Text = "";
484 txtProductID.Text = "";
485 txtPrice.Text = "";
486 txtAvailableQty.Text = "";
487 txtSaleQty.Text = "";
488 txtTotalAmount.Text = "";
489 ListView1.Items.Clear();
490 txtDiscountAmount.Text = "";
491 txtDiscountPer.Text = "";
492
493 txtSubTotal.Text = "";
494 txtTaxPer.Text = "";
495 txtTaxAmt.Text = "";
496 txtTotal.Text = "";
497 txtTotalPayment.Text = "";
498 txtPaymentDue.Text = "";
499 txtProduct.Text = "";
500 Save.Enabled = true;
501 btnRemove.Enabled = false;
502 btnPrint.Enabled = false;
503 ListView1.Enabled = true;
504 Button7.Enabled = true;
505
506 }
507
508 private void NewRecord_Click(object sender, EventArgs e)
509 {
510 Reset();
511 Reset();
512 }
513
514
515 private void txtTotalPayment_TextChanged(object sender, EventArgs e)
516 {
517 int val1 = 0;
518 int val2 = 0;
519 int.TryParse(txtTotal.Text, out val1);
520 int.TryParse(txtTotalPayment.Text, out val2);
521 int I = (val1 - val2);
522 txtPaymentDue.Text = I.ToString();
523 }
524
525 private void txtTotalPayment_Validating(object sender, CancelEventArgs e)
526 {
527 int val1 = 0;
528 int val2 = 0;
529 int.TryParse(txtTotal.Text, out val1);
530 int.TryParse(txtTotalPayment.Text, out val2);
531 if (val2 > val1)
532 {
533 MessageBox.Show("Total Payment can't be more than grand total", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
534 txtTotalPayment.Text = "";
535 txtPaymentDue.Text = "";
536 txtTotalPayment.Focus();
537 return;
538 }
539 }
540
541 private void txtSaleQty_Validating(object sender, CancelEventArgs e)
542 {
543
544 int val1 = 0;
545 int val2 = 0;
546 int.TryParse(txtAvailableQty.Text, out val1);
547 int.TryParse(txtSaleQty.Text, out val2);
548 if (val2 > val1)
549 {
550 MessageBox.Show("Selling quantities are more than available quantities", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
551 txtSaleQty.Text = "";
552 txtTotalAmount.Text = "";
553 txtSaleQty.Focus();
554 return;
555 }
556 }
557
558 private void btnPrint_Click(object sender, EventArgs e)
559 {
560 try
561 {
562 Cursor = Cursors.WaitCursor;
563 timer1.Enabled = true;
564 rptInvoice rpt = new rptInvoice();
565 //The report you created.
566 cmd = new SqlCommand();
567 SqlDataAdapter myDA = new SqlDataAdapter();
568 POS_DBDataSet myDS = new POS_DBDataSet();
569 //The DataSet you created.
570 con = new SqlConnection(cs.DBConn);
571 cmd.Connection = con;
572 cmd.CommandText = "SELECT * from product,invoice_info,productsold,customer where invoice_info.invoiceno=productsold.invoiceno and invoice_info.customerID=Customer.CustomerID and ProductSold.ProductID=Product.ProductID and Invoice_info.invoiceNo='" + txtInvoiceNo.Text + "'";
573 cmd.CommandType = CommandType.Text;
574 myDA.SelectCommand = cmd;
575 myDA.Fill(myDS, "product");
576 myDA.Fill(myDS, "Invoice_Info");
577 myDA.Fill(myDS, "ProductSold");
578 myDA.Fill(myDS, "Customer");
579 rpt.SetDataSource(myDS);
580 frmInvoiceReport frm = new frmInvoiceReport();
581 frm.crystalReportViewer1.ReportSource = rpt;
582 frm.Visible=true;
583 }
584 catch (Exception ex)
585 {
586 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
587 }
588 }
589
590 private void timer1_Tick(object sender, EventArgs e)
591 {
592 Cursor = Cursors.Default;
593 timer1.Enabled = false;
594 }
595
596
597
598 private void txtSaleQty_KeyPress(object sender, KeyPressEventArgs e)
599 {
600 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
601 {
602 e.Handled = false;
603 }
604 else
605 {
606 e.Handled = true;
607 }
608 }
609
610 private void txtTotalPayment_KeyPress(object sender, KeyPressEventArgs e)
611 {
612 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
613 {
614 e.Handled = false;
615 }
616 else
617 {
618 e.Handled = true;
619 }
620 }
621
622 private void txtTaxPer_KeyPress(object sender, KeyPressEventArgs e)
623 {
624 // allows 0-9, backspace, and decimal
625 if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46))
626 {
627 e.Handled = true;
628 return;
629 }
630 }
631
632 private void txtDiscountPer_TextChanged(object sender, EventArgs e)
633 {
634 Calculate();
635 }
636 }
637 }